package cn.com.smartdevices.bracelet.chart.typeface;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

/* renamed from: cn.com.smartdevices.bracelet.chart.typeface.c */
public class TypefaceManager { // C0783c
    private static final TypefaceManager f4038a;
    private final HashMap<TextStyle, WeakReference<Typeface>> f4039b;
    private final HashSet<TextStyleExtractor> f4040c;

    static {
        f4038a = new TypefaceManager();
    }

    private TypefaceManager() {
        this.f4039b = new HashMap();
        this.f4040c = new HashSet();
    }

    public static TypefaceManager m8722a() {
        return f4038a;
    }

    public static void m8723a(TextStyleExtractor c0781e) {
        f4038a.f4040c.add(c0781e);
    }

    public Typeface m8724a(Context context, TextStyle c0779d) {
        Typeface typeface = null;
        Typeface typeface2 = null;
        if (c0779d != null) {
            if (this.f4039b.containsKey(c0779d)) {
                typeface2 = (Typeface) ((WeakReference) this.f4039b.get(c0779d)).get();
                if (typeface2 == null) {
                    typeface = typeface2;
                }
            } else {
                typeface = null;
            }
            try {
                typeface2 = Typeface.createFromAsset(context.getAssets(), c0779d.getFontName());
            } catch (Exception e) {
                e.printStackTrace();
                typeface2 = typeface;
            }
            if (typeface2 != null) {
                this.f4039b.put(c0779d, new WeakReference(typeface2));
            }
        }
        return typeface2;
    }

    public Typeface getTypeface(Context var1, TextStyle var2) {  // m8724a
        Typeface var3 = null;
        if(var2 != null) {
            Typeface var4;
            if(this.f4039b.containsKey(var2)) {
                var3 = (Typeface)((WeakReference)this.f4039b.get(var2)).get();
                if(var3 != null) {
                    return var3;
                }

                var4 = var3;
            } else {
                var4 = null;
            }

            label20: {
                Typeface var7;
                try {

                    var7 = Typeface.createFromAsset(var1.getAssets(), var2.getFontName());
                } catch (Exception var8) {
                    var8.printStackTrace();
                    var3 = var4;
                    break label20;
                }

                var3 = var7;
            }

            if(var3 != null) {
                this.f4039b.put(var2, new WeakReference(var3));
                return var3;
            }
        }
        return var3;
    }

    public Typeface m8725a(Context context, String str) {
        if (!TextUtils.isEmpty(str)) {
            Iterator it = this.f4040c.iterator();
            while (it.hasNext()) {
                TextStyle a = ((TextStyleExtractor) it.next()).m8718a(str);
                if (a != null) {
                    return getTypeface(context, a);
                }
            }
        }
        return null;
    }

    public void m8726a(TextView textView, Context context, AttributeSet attributeSet) {
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, new int[]{16843692});
        m8728a(textView, obtainStyledAttributes.getString(0));
        obtainStyledAttributes.recycle();
    }

    public void m8727a(TextView textView, TextStyle c0779d) {
        Typeface a = getTypeface(textView.getContext(), c0779d);
        if (a != null) {
            textView.setTypeface(a);
        }
    }

    public void m8728a(TextView textView, String str) {
        Typeface a = m8725a(textView.getContext(), str);
        if (a != null) {
            textView.setTypeface(a);
        }
    }
}
